package com.ruijie.camerax.media;

import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.os.Build;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * author: luqihua
 * date:2019-11-11
 * description:
 **/
public class MediaFormatCreate {
    private static final String TAG = "MediaFormatCreate";
    private static final String MIME_TYPE = "video/avc";
    private static final Integer BIT_RATE = 500 * 1024 * 8;//码率500kb/s
    private static final Integer FRAME_RATE = 18;//帧率
    public static final Integer COLOR_FORMAT;//颜色编码

    static {
        COLOR_FORMAT = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible;
    }

    public static MediaFormat createFormat(String mime, int width, int height, int orientation, boolean useSurface) {
        MediaFormat mediaFormat = MediaFormat.createVideoFormat(mime, width, height);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
        mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
        if (useSurface) {
            mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
        } else {
            mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, COLOR_FORMAT);
        }
        mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 25);//关键帧时间
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mediaFormat.setInteger(MediaFormat.KEY_ROTATION, orientation);
        }
        return mediaFormat;
    }

    /**
     * 找到最合适的颜色编码
     *
     * @return
     */
    public static int getSuitableColorFormat() {
        MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
        MediaCodecInfo[] codecInfos = mediaCodecList.getCodecInfos();
        MediaCodecInfo codecInfo = null;
        for (MediaCodecInfo info : codecInfos) {
            String[] supportedTypes = info.getSupportedTypes();
            for (String supportedType : supportedTypes) {
                if (supportedType.equalsIgnoreCase(MIME_TYPE)) {
                    codecInfo = info;
                    break;
                }
            }
        }
        int colorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible;
        if (codecInfo != null) {
            MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(MIME_TYPE);
            if (capabilities != null) {
                int[] colorFormats = capabilities.colorFormats;
                List<Integer> ints = new ArrayList<>();

                for (int format : colorFormats) {
                    Log.d(TAG, "showSupportColorFormat: " + format);
                    ints.add(format);
                }
                //优选i420   format=19
                if (ints.contains(MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar)) {
                    colorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar;
                } else if (ints.contains(MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar)) {
                    //第二选NV21  format=21
                    colorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar;
                }
            }
        }
        Log.d(TAG, "getSuitableColorFormat: " + colorFormat);
        return colorFormat;
    }


    public static void showSupportColorFormat(MediaCodecInfo codecInfo) {
        MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(MIME_TYPE);
        if (capabilities != null) {
            Log.d(TAG, "showSupportColorFormat: "+ capabilities.getMaxSupportedInstances());
            int[] colorFormats = capabilities.colorFormats;
            for (int format : colorFormats) {
                Log.d(TAG, "showSupportColorFormat: " + format);
            }
        }
    }
}
